package com.xm.adv.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xm.adv.R;
import com.xm.adv.bean.OpenCloseBean;

import java.text.ParseException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import xinya.com.baselibrary.utils.DateTransforam;
import xinya.com.baselibrary.utils.ToastUtils;
import xinya.com.baselibrary.view.WheelView;

/**
 * Created by zhangjiebo on 2018/4/27.
 *
 * @author ZhangJieBo
 */
public class TimeDialog extends Dialog {
    private String hourStart = "00";
    private String mintuneStart = "00";
    private String hourEnd = "00";
    private String mintuneEnd = "00";
    private Context context;
    OpenCloseBean openCloseBean;
    private ViewHolder viewHolder;

    public interface OnChooseListener {
        void choose(String hourStart, String mintuneStart, String hourEnd, String mintuneEnd);
    }

    OnChooseListener onChooseListener;

    public TimeDialog(Context context, OpenCloseBean openCloseBean, OnChooseListener onChooseListener) {
        super(context, R.style.dialog);
        this.context = context;
        this.openCloseBean = openCloseBean;
        this.onChooseListener = onChooseListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_single_wheel, null);
        setContentView(view);
        viewHolder = new ViewHolder(view);
        initData();

        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.dialogFenXiang);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }


    private void initData() {
        int start01 = Integer.parseInt(openCloseBean.getStartTime().split(":")[0]);
        int start02 = Integer.parseInt(openCloseBean.getStartTime().split(":")[1]);
        int end01 = Integer.parseInt(openCloseBean.getEndTime().split(":")[0]);
        int end02 = Integer.parseInt(openCloseBean.getEndTime().split(":")[1]);

        ArrayList<String> stringList1 = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                stringList1.add("0" + i);
            } else {
                stringList1.add("" + i);
            }
        }
        viewHolder.wheelView1.setData(stringList1);
        viewHolder.wheelView1.setDefault(start01);
        viewHolder.wheelView1.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                hourStart = text;
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        ArrayList<String> stringList2 = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                stringList2.add("0" + i);
            } else {
                stringList2.add("" + i);
            }
        }
        viewHolder.wheelView2.setData(stringList2);
        viewHolder.wheelView2.setDefault(start02);
        viewHolder.wheelView2.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                mintuneStart = text;
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        ArrayList<String> stringList3 = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                stringList3.add("0" + i);
            } else {
                stringList3.add("" + i);
            }
        }
        viewHolder.wheelView3.setData(stringList3);
        viewHolder.wheelView3.setDefault(end01);
        viewHolder.wheelView3.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                hourEnd = text;
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        ArrayList<String> stringList4 = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                stringList4.add("0" + i);
            } else {
                stringList4.add("" + i);
            }
        }
        viewHolder.wheelView4.setData(stringList4);
        viewHolder.wheelView4.setDefault(end02);
        viewHolder.wheelView4.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                mintuneEnd = text;
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        viewHolder.textCancle.setOnClickListener(view -> dismiss());
        viewHolder.textSure.setOnClickListener(view -> {
            try {
                long startStamp = DateTransforam.dateToStampXXX(hourStart + ":" + mintuneStart);
                long endStamp = DateTransforam.dateToStampXXX(hourEnd + ":" + mintuneEnd);
                if (Math.abs(startStamp - endStamp) <= 5 * 60 * 1000L) {
                    ToastUtils.showToast(context, "开机时间和关机时间之差要大于5分钟");
                    return;
                }
            } catch (ParseException e) {
                e.printStackTrace();
                return;
            }
            dismiss();
            if (onChooseListener != null) {
                onChooseListener.choose(hourStart, mintuneStart, hourEnd, mintuneEnd);
            }
        });
    }

    static
    class ViewHolder {
        @BindView(R.id.textCancle)
        TextView textCancle;
        @BindView(R.id.textSure)
        TextView textSure;
        @BindView(R.id.wheelView1)
        WheelView wheelView1;
        @BindView(R.id.wheelView2)
        WheelView wheelView2;
        @BindView(R.id.wheelView3)
        WheelView wheelView3;
        @BindView(R.id.wheelView4)
        WheelView wheelView4;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
